package com.hb.game.pk.ui.activity.mine.recommend;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hb.game.pk.R;
import com.hb.game.pk.adpter.RecommendListListAdapter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.TJBean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyTeatActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    Call<TJBean> call;
    List<TJBean.DataBeanX.DataBean> data;
    int counts;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recommend_list;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyTeatActivity.this.finish();
            }
        });
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        Intent intent = getIntent();
        String user_id = intent.getStringExtra("user_id");
        counts = intent.getIntExtra("counts", 0);
        String tilte = (counts + 1) + "级团队";
        titleBar.setTitle(tilte);
        if (counts != 0) {
            counts++;
            call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).tjList(user_id, counts);
            call.enqueue(new Callback<TJBean>() {
                @Override
                public void onResponse(Call<TJBean> call, Response<TJBean> response) {
                    TJBean body = response.body();
                    if (body == null) {
                        ToastUtils.showToast("请求错误");
                        return;
                    }
                    if (body.getStatus() != 1) {
                        ToastUtils.showToast(body.getInfo());
                        return;
                    }
                    data = body.getData().getData();
                    titleBar.setRightText(body.getData().getCount() + "人");
                    initAdapter();
                }

                @Override
                public void onFailure(Call<TJBean> call, Throwable t) {
                    ToastUtils.showToast("网络请求错误");
                }
            });
        } else {
            ToastUtils.showToast("数据有误");
        }
    }

    @Override
    protected void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new RecommendListListAdapter(R.layout.item_recommend_list_list);
            mAdapter.setNewData(data);
            if (counts < 28)
                mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        TJBean.DataBeanX.DataBean dataBean = data.get(position);
                        String user_id = dataBean.getUser_id();
                        Intent intent = new Intent();
                        intent.putExtra("user_id", user_id);
                        intent.putExtra("counts", counts);
                        startActivity(MyTeatActivity.class, intent);
                    }
                });
        } else {
            mAdapter.setNewData(data);
        }
        super.initAdapter();
    }

    @Override
    public boolean isNeedLoadMore() {
        return false;
    }
}
